package com.support.android.myapp3.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.support.android.myapp3.R;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Test2Fragment extends Fragment {
    private View view;
    private ListView listView;
    private static Test2Fragment fa;

    public static Test2Fragment getInstance() {
        if (fa == null) {
            fa = new Test2Fragment();
        }
        return fa;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view= inflater.inflate(R.layout.fragment_tax_cardview, container, false);
        return   view;
    }

    public void setListViewHeightBasedOnChildren(ListView listView) {
        // 获取ListView对应的Adapter
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            return;
        }

        int totalHeight = 45 ;
        for (int i = 0, len = listAdapter.getCount(); i < len; i++) {
            // listAdapter.getCount()返回数据项的数目
            View listItem = listAdapter.getView(i, null, listView);
            // 计算子项View 的宽高
            listItem.measure(0, 0);
            // 统计所有子项的总高度
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        // listView.getDividerHeight()获取子项间分隔符占用的高度
        // params.height最后得到整个ListView完整显示需要的高度
        listView.setLayoutParams(params);
    }

    public List<Map<String, String>> calcListData(String price) {
        List<Map<String, String>> data = new ArrayList<>();
        float step = 0.5f;
        float init = 0;

        while (init < 10) {
            init += 0.5;
            HashMap<String, String> item = new HashMap<String, String>();
            item.put("p_down", calcPriceDown(price,init));
            item.put("p_up", calcPriceUp(price,init));
            data.add(item);

        }

        return data;
    }

    public String calcPriceUp(String price, float step) {
        BigDecimal p = new BigDecimal(price);
        BigDecimal s = new BigDecimal(step);

        return p.multiply(s.divide(new BigDecimal("100"))).add(p).
                setScale(3, BigDecimal.ROUND_HALF_DOWN).toString() + String.format("(+%s%%)", step);
    }

    public String calcPriceDown(String price, float step) {
        BigDecimal p = new BigDecimal(price);
        BigDecimal s = new BigDecimal(step);

        return p.subtract(p.multiply(s.divide(new BigDecimal("100")))).
                setScale(3, BigDecimal.ROUND_HALF_DOWN).toString() + String.format("(-%s%%)", step);
    }

}
